package com.woniuxy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.woniuxy.config.UploadConfigDate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;


/**
 * 阿里云上传
 *
 * @author 周川雄
 * @date 2022/1/3 20:38
 */
@Component
public class OSSClientUtil {
    @Resource
    private UploadConfigDate uploadConfigDate;

    // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com
//    private String endpoint = "oss-cn-chengdu.aliyuncs.com";
//    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
//    private String accessKeyId = "LTAI5tRTdzhMSgrmrJcUWj9n";
//    private String accessKeySecret = "vxxZw4KNZG43mhFnKA1ldcJssKmHPW";

    /**
     * 阿里云上传
     *
     * @param key   文件名字
     * @param input 流
     * @return
     */
    public String uploadFile(String key, InputStream input) {
        String bucketName = uploadConfigDate.getBucketName();
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(uploadConfigDate.getEndPoint(), uploadConfigDate.getAccessKeyId(), uploadConfigDate.getAccessKeySecret());
            ossClient.putObject(bucketName, key, input);
        } catch (OSSException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
        return "https://" + bucketName + "." + uploadConfigDate.getEndPoint() + File.separator + key;
    }

    /**
     * 文件下载
     *
     * @param src            本地路径
     * @param yourObjectName
     */
    public void downFile(String src, String yourObjectName) {

        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(uploadConfigDate.getEndPoint(), uploadConfigDate.getAccessKeyId(), uploadConfigDate.getAccessKeySecret());

            // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
            File file = new File(src);

            if (!file.exists()) {//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            ossClient.getObject(new GetObjectRequest(uploadConfigDate.getBucketName(), yourObjectName), new File(file + yourObjectName));

        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }


    }


    /**
     * @param key 文件名
     */
    public void deleteFile(String key) {
        String bucketName = uploadConfigDate.getBucketName();
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(uploadConfigDate.getEndPoint(), uploadConfigDate.getAccessKeyId(), uploadConfigDate.getAccessKeySecret());

            if (!ossClient.doesBucketExist(bucketName)) {
                System.out.println("=>bucket不存在");
                return;
            } else {
                System.out.println("=>开始删除");
                ossClient.deleteObject(bucketName, key);
                System.out.println("===========>删除成功");
            }
        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
    }
}
